package com.hnu.highlynewapp.service.impl;

import com.hnu.highlynewapp.entity.ContactEntity;
import com.hnu.highlynewapp.entity.ServiceProductEntity;
import com.hnu.highlynewapp.service.ContactService;
import com.hnu.highlynewapp.vo.PublishServiceProductVo;
import com.hnu.highlynewapp.vo.PublishTechAchVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.Query;

import com.hnu.highlynewapp.dao.TechnologyAchievementDao;
import com.hnu.highlynewapp.entity.TechnologyAchievementEntity;
import com.hnu.highlynewapp.service.TechnologyAchievementService;


@Service("technologyAchievementService")
public class TechnologyAchievementServiceImpl extends ServiceImpl<TechnologyAchievementDao, TechnologyAchievementEntity> implements TechnologyAchievementService {

    @Autowired
    private ContactService contactService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TechnologyAchievementEntity> page = this.page(
                new Query<TechnologyAchievementEntity>().getPage(params),
                new QueryWrapper<TechnologyAchievementEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据条件查询技术成果
     * @param duration
     * @param field
     * @return
     */
    @Override
    public List<TechnologyAchievementEntity> listByCondition(String duration, String field) {
        List<TechnologyAchievementEntity> byDuration = new ArrayList<>();
        List<TechnologyAchievementEntity> byField = new ArrayList<>();
        int i = 0;
        switch (duration) {
            case "一个月内":
                i = 30;
                break;
            case "三个月内":
                i = 90;
                break;
            case "六个月内":
                i = 180;
                break;
            case "一年内":
                i = 365;
                break;
            default:
                i = -1;
                break;
        }
        List<TechnologyAchievementEntity> result = new ArrayList<>();
        if ("全部".equals(duration)&&"全部".equals(field)) {
            QueryWrapper<TechnologyAchievementEntity> wrapper= new QueryWrapper<>();
            wrapper.orderByDesc("publish_time");
            result = this.list(wrapper);
        } else if (!"全部".equals(duration)&&"全部".equals(field)){
            result = this.getBaseMapper().listByDuration(i);
        } else if ("全部".equals(duration)&&!"全部".equals(field)) {
            result = this.getBaseMapper().listByField(field);
        } else {
            result = this.getBaseMapper().listByBoth(i,field);
        }
        return result;
    }
    @Override
    public void publish(PublishTechAchVo vo) {
        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(vo,contactEntity);
        Long contactId = contactService.saveAndCheckExist(contactEntity);

        TechnologyAchievementEntity technologyAchievementEntity = new TechnologyAchievementEntity();
        BeanUtils.copyProperties(vo,technologyAchievementEntity);
        technologyAchievementEntity.setPublishTime(new Date());
        technologyAchievementEntity.setContactId(contactId);
        baseMapper.insert(technologyAchievementEntity);
    }

    @Override
    public List<TechnologyAchievementEntity> listByPublisherID(long publisherID, Integer publisherType) {
        QueryWrapper<TechnologyAchievementEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("publisher_id",publisherID)
                .eq("publisher_type",publisherType).orderByDesc("publish_time");
        List<TechnologyAchievementEntity> list = this.list(wrapper);
        return list;
    }
}